package org.dru.clay.respository.transport.ssh;

import java.io.File;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SwingUserInfo implements SshUserInfo {
	private static final String TITLE = "JsClay SSH Transport";

	private final ImageIcon clayIcon;
	private String password;
	private String passphrase;
	private String username;
	private String identity;

	public SwingUserInfo() {
		final URL url = getClass().getClassLoader().getResource("clay-icon-32.png");
		clayIcon = new ImageIcon(url);
	}

	@Override
	public String getPassphrase() {
		return passphrase;
	}

	@Override
	public String getPassword() {
		return password;
	}
	
	@Override
	public String getUsername() {
		return username;
	}
	
	@Override
	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public boolean promptPassphrase(String message) {
		final JPasswordField field = new JPasswordField(20);
		final int result = JOptionPane.showConfirmDialog(null, new Object[] { new JLabel(message), field }, TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, clayIcon);
		if (result == JOptionPane.OK_OPTION) {
			passphrase = new String(field.getPassword());
			return true;
		}
		return false;
	}

	@Override
	public boolean promptPassword(String message) {
		final JPasswordField field = new JPasswordField(20);
		final int result = JOptionPane.showConfirmDialog(null, new Object[] { new JLabel(message), field }, TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, clayIcon);
		if (result == JOptionPane.OK_OPTION) {
			password = new String(field.getPassword());
			return true;
		}
		return false;		
	}
	
	@Override
	public boolean promptUsername(String message) {
		final JTextField field = new JTextField(20);
		final int result = JOptionPane.showConfirmDialog(null, new Object[] { new JLabel(message), field }, TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, clayIcon);
		if (result == JOptionPane.OK_OPTION) {
			username = field.getText();
			return true;
		}
		return false;		
	}

	@Override
	public boolean promptYesNo(String message) {
		final int n = JOptionPane
				.showConfirmDialog(null, message, TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, clayIcon);
		return n == 0;
	}

	@Override
	public void showMessage(String message) {
		JOptionPane.showMessageDialog(null, message, TITLE, JOptionPane.INFORMATION_MESSAGE, clayIcon);
	}

	@Override
	public String getIdentity() {
		return identity;
	}

	@Override
	public boolean promptForIdentity(String message) {		
		final JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory(new File("."));
		fc.setDialogTitle(message);
		fc.setFileHidingEnabled(false);
		int response = fc.showDialog(null, "Select");
		if (response == JFileChooser.APPROVE_OPTION) {
			identity = fc.getSelectedFile().getAbsolutePath();
			return true;
		}
		return false;
	}
}
